'use strict';


let rtc={};
let connections={};
let PeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection;
let SessionDescription = window.RTCSessionDescription || window.mozRTCSessionDescription || window.webkitRTCSessionDescription;

function getConnct(uid) {
    let connection=connections[uid];
    if(connection==null){
        let configuration = {
            iceServers : [{
                urls: "turn:47.108.85.176:3478",
                username:"linzi",
                credential:"jzl110120119"
            }]
        };
        connection=new PeerConnection(configuration);
        connections[uid]=connection;
    }
    return connection;
}

rtc.init=function init(uid,iceBack,trackBack){
    let connection=getConnct(uid);
    connection.onicecandidate=function (event) {
        if(event.candidate){
            iceBack(event.candidate);
        }
    };
    connection.onaddstream=function (obj) {
        trackBack(obj);
    };
}

rtc.responseOffer=function start(stream,uid,offer){
    let connection=getConnct(uid);
    connection.addStream(stream);
    connection.setRemoteDescription(new SessionDescription({
        type:'offer',
        sdp:offer
    }));
};

rtc.createAnswer=function call(uid,ansCreate){
    let connection=getConnct(uid);
    let options = {
        offerToReceiveAudio:true,
        offerToReceiveVideo:true
    };
    connection.createAnswer(options).then(function (answer) {
        console.log('answer',answer);
        connection.setLocalDescription(answer);
        ansCreate(answer);
    },function error(err) {
        console.log('answer创建错误',err);
    });
};
rtc.createOffer=function call(stream,uid,offCreate){
    let connection=getConnct(uid);

    connection.addStream(stream);
    console.log("连接状态",connection.connectionState);
    let options = {
        offerToReceiveAudio:true,
        offerToReceiveVideo:true
    };
    connection.createOffer(options).then(function (offer) {
        connection.setLocalDescription(offer);
        offCreate(offer);
    },function error(err) {

    });
};
rtc.responseAnswer=function responseAnswer(uid,answer){
    let connection=getConnct(uid);
    connection.setRemoteDescription(new SessionDescription({
        type:'answer',
        sdp:answer
    }));
};

rtc.responseIceDate=function iceResponse(uid,mid,lab,ice){
    let connection=getConnct(uid);
    let candidate = new RTCIceCandidate({
        sdpMLineIndex:lab,
        candidate:ice,
        sdpMid:mid
    });
    connection.addIceCandidate(candidate);
};
rtc.toOtherStream=function toOther(uid,event){
    let connection=getConnct(uid);
    event.forEach(track=>{
        connection.addTrack(track);
    });
};

rtc.close=function close(uid) {
    if(uid!=null){
        let connection=connections[uid];
        connection.close();
        delete connections[uid];
    }else{
        for(let key in connections){
            connections[key].close();
        }
        for(let key in connections){
            delete connections[key];
        }
    }
};
// module.exports=rtc;